$(function() {
    obj.init();
});

var obj = new function () {
    return {
        params: {
            type: 'dep',
            goal: null,
            goalId: null,
            target: null,
            manage: 0
        },
        dom: {
            list: null,
            // save: null
        },
        init: function () {
            var $this = this;

            this.params.goal   = xhTool.getQuery('goal');
            this.params.goalId = xhTool.getQuery('goalId');
            this.params.target = xhTool.getQuery('target');
            this.params.manage = xhTool.getQuery('manage');

            // this.dom.save = window.top.xhTop.getModalConfirmDom();
            this.dom.list = $('#list');
            this.dom.list.treegrid({
                url: baseUrl + 'hrm/department/getDep',
                onLoadSuccess: function (data) {
                    $this.funObj().selectExistPower();
                },
                onSelect: function () {
                    $this.funObj().savePower();
                },
                onUnselect: function () {
                    $this.funObj().savePower();
                },
                onSelectAll: function () {
                    $this.funObj().savePower();
                },
                onUnselectAll: function () {
                    $this.funObj().savePower();
                }
            });

            this.listener();
        },
        listener: function () {
            var $this = this;
            //保存权限
            $this.dom.save.click(function () {
                 $this.funObj().savePower();
            });
        },
        funObj: function () {
            var $this = this;
            return {
                selectExistPower: function () {
                    xhExtra.ajaxRequest('Power/getExtraPower', $this.params, {check: true, success: false}, function (data) {
                        $.each(data.powerIds, function (index, id) {
                            $this.dom.list.treegrid('select', id);
                        });
                    });
                },
                savePower: function () {
                    var rows = $this.dom.list.treegrid('getSelections'),
                        ids  = [];
                    for(var i=0; i < rows.length; i++){
                        ids.push(rows[i].iDepId);
                    }
                    // xhExtra.ajaxRequest('Power/saveExtraPower', $.extend({}, $this.params, {ids: ids}), {check: false, success: true}, function (result) {
                    //     if (xhExtra.checkError(result)) {
                    //         xhExtra.setBtnNormal($this.dom.save);
                    //         return false;
                    //     }
                    //     window.top.xhTop.closeModal();
                    // });
                    xhExtra.ajaxRequest('Power/saveExtraPower', $.extend({}, $this.params, {ids: ids}), {check: true, success: false});
                }
            }
        }
    }
};